// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router'
import axios from 'axios'
import Vuex from 'vuex'
Vue.use(Vuex)
import store from './vuex/store'



// 引入时间格式化
import filters from "../src/utils/filters";
// ------------------------------------------------------
// 添加时间过滤器
Object
  .keys(filters)
  .forEach(key => {
    Vue.filter(key, filters[key]);
  });

Date.prototype.format = function (format) {
  var o = {
    "M+": this.getMonth() + 1, //month
    "d+": this.getDate(), //day
    "h+": this.getHours(), //hour
    "m+": this.getMinutes(), //minute
    "s+": this.getSeconds(), //second
    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
    "S": this.getMilliseconds() //millisecond
  }
  if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(format))
      format = format.replace(RegExp.$1,
        RegExp.$1.length == 1 ? o[k] :
          ("00" + o[k]).substr(("" + o[k]).length));
  return format;
}


// -----------------------------------------------------------------

function request({
  method,
  url,
  params,
  customErr = false
}) {
  return new Promise((resolve, reject) => {
    axios({
      method: method,
      url: url,
      [method === "post" ?
        "data" :
        "params"
      ]: params
    }).then(response => {
      if (customErr) {
        resolve(response.data);
      } else {
        if (response.data.code === 0) {
          resolve(response.data.data);
        } else {
          alert('error')
        }
      }
    }, error => {
      alert('error');
      reject(error)
    }).catch(error => {
      alert('error');
      reject(error)
    });
  });
}

// ------------------------------------------------------
// 将请求方法设置为全局属性
Vue.prototype.$http = request;
Vue.use(VueRouter)
Vue.config.productionTip = false
Vue.use(ElementUI);
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: {
    App
  },
  template: '<App/>'
})
